<template>
    <div v-show="showDialog">
        <div class="add-address-show">
            <div class="title">
                <div>清单导出</div>
                <img :src="$global.Image.icon_close" @click="_cancelDialog" />
            </div>
            <div class="add-input">
                <el-form :model="addressForm" :rules="rules" ref="addressForm" label-width="100px" class="rule-form">
                    <el-form-item label="订单名称：" prop="order_name">
                        <el-input v-model="addressForm.order_name" placeholder="订单名称" readonly></el-input>
                    </el-form-item>
                    <el-form-item label="订单编号：" prop="order_id">
                        <el-input v-model="addressForm.order_id" placeholder="订单编号" readonly></el-input>
                    </el-form-item>
                    <el-form-item label="收件邮箱：" prop="email">
                        <el-input v-model="addressForm.email" placeholder="请输入收件邮箱"></el-input>
                    </el-form-item>
                    <div class="history" v-if="historyEmail.length">
                        <div class="history-list">
                            <div class="" v-for="(item,index) in historyEmail" :key="index" @click="historyClick(item)">
                                <img @click.stop="delHistory(index)" src="../../assets/second_icon/close_icon.png" alt="">
                                {{item}}
                            </div>
                        </div>
                    </div>
                    <div class="td-fbc" style="margin-left: 100px;margin-bottom: 20px;">
                        <el-button class="btn-b" @click="_cancelDialog">取消</el-button>
                        <el-button class="btn-r" type="primary" style="color:#FFFFFF !important" @click="submitForm('addressForm')">确认</el-button>
                    </div>
                </el-form>
            </div>
        </div>
        <div class="dialog-shade"></div>
    </div>
</template>

<script>
import { exportProduct } from "@api/user";

export default {
    name: "orderName",
    props: ["approveType"],
    watch: {
        showDialog(e) {
            if (e) {
                if (localStorage.getItem("historyEmail")) {
                    this.historyEmail = JSON.parse(
                        localStorage.getItem("historyEmail")
                    );
                }
            }
        },
    },
    data() {
        return {
            showDialog: false,
            historyEmail: [],
            addressForm: {
                id: "",
                order_name: "",
                order_id: "",
                email: "",
            },
            rules: {
                email: [
                    {
                        required: true,
                        message: "请输入邮箱",
                        trigger: "blur",
                    },
                ],
            },
            callback: null,
        };
    },
    mounted() {},
    methods: {
        delHistory(index) {
            this.historyEmail.splice(index,1)
            let arr = this.historyEmail;
            
            localStorage.setItem("historyEmail", JSON.stringify(arr));
            console.log(this.historyEmail,arr)
        },
        historyClick(e) {
            this.addressForm.email = e;
        },
        // 打开编辑
        open(item, callback) {
            this.init();
            this.callback = callback;
            this.orderInfo = item;
            this.addressForm.order_name = item.order_name;
            this.addressForm.order_id = item.order_id;
            this.addressForm.id = item.id;
            this.showDialog = true;
        },
        // 初始化
        init() {
            this.addressForm.order_name = "";
            this.addressForm.order_id = "";
            this.addressForm.id = "";
            this.addressForm.email = "";
        },
        _cancelDialog() {
            this.init();
            this.showDialog = false;
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        async submit() {
            let { id, email } = this.addressForm;
            try {
                let that = this,
                    params = {
                        type: 1,
                        id: id,
                        email: email,
                    };
                exportProduct(params)
                    .then((res) => {
                        console.log(res.status);
                        if (res.status != 200) {
                            that.$dialog.toast({ mes: res.msg });
                            return;
                        }
                        let arr =
                            JSON.parse(localStorage.getItem("historyEmail")) ||
                            [];

                        arr = arr.concat(email);
                        localStorage.setItem(
                            "historyEmail",
                            JSON.stringify(arr)
                        );
                        that.$dialog.toast({
                            mes: "导出成功，请注意查收邮件。",
                        });
                        that.showDialog = false;
                        that.callback();
                    })
                    .catch((err) => {
                        that.$dialog.error(err.msg);
                    });
            } catch (e) {
                this.$dialog.error(e.msg);
            }
        },
    },
};
</script>
<style >
.el-input {
    width: 100%;
}
.add-address-show .rule-form {
    width: auto;
}
</style>
<style scoped  rel="stylesheet/scss" lang="scss">
$color: #da213d;
.for-itm {
    margin-left: -50px;
    margin-top: 50px;
    .el-form-item__content {
        margin-left: 0 !important;
        width: 540px;
        display: flex;
        justify-content: space-between;
    }
    .btn-r {
        width: 240px;
        height: 44px;
        background: #da213d;
        font-size: 16px;
        border-radius: 5px;
    }
    .btn-b {
        width: 242px;
        height: 46px;
        border: 1px solid #da213d !important;
        border-radius: 5px;
        font-size: 16px;
        color: #da213d !important;
        background: #fff !important;
    }
}
.add-address-show {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 700px;
    border-radius: 4px;
    background-color: rgba(255, 255, 255, 1);
    box-shadow: 0 0 15px 4px rgba(0, 0, 0, 0.2);
    margin: auto;
    z-index: 20;
    .title {
        display: flex;
        width: 100%;
        height: 67px;
        align-items: center;
        background: #fbf0f0;
        justify-content: center;
        > div:nth-of-type(1) {
            font-size: 21px;
            color: #da213d;
            font-weight: bold;
        }
        img {
            display: block;
            width: 20px;
            height: 20px;
            font-size: 30px;
            color: #999999;
            position: absolute;
            right: 32px;
            cursor: pointer;
        }
    }
    .add-input {
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        padding: 60px 0;
        box-sizing: border-box;
    }
    .rule-form {
        width: 460px;
        button {
            width: 140px;
        }
    }
}
.history {
    margin-bottom: 20px;
    .history-list {
        display: flex;
        flex-wrap: wrap;
        div {
            border-radius: 10px;
            padding: 5px;
            box-sizing: border-box;
            font-size: 16px;
            line-height: 16px;
            color: #999;
            background: #f8f8f8;
            margin: 0px 5px 5px;
            cursor: pointer;
            position: relative;
            img {
                position: absolute;
                right: -10px;
                top: -10px;
                width: 20px;
                height: 20px;
            }
        }
    }
}
.dialog-shade {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 18;
    background-color: rgba($color: #000000, $alpha: 0.4);
}

.el-cascader {
    display: block;
    position: absolute;
    top: 0;
    width: 100%;
    opacity: 0;
}
</style>